package main;

import html.EditHtml;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Enumeration;
import java.util.Properties;
import xml.EditXml;
public class EditConfig
{
     public static void main(String[] args) throws Exception
    {
         if(args.length==1)
         {
              try
             {
                 String propFile=args[0].trim();
                 Properties prop=new Properties();
                 prop.load(new FileInputStream(propFile+".properties"));
                 Enumeration keys=prop.keys();
                 String fname=prop.getProperty(args[0]);
                 System.out.println("file: "+fname);                 
                 int dotLoc=fname.lastIndexOf(".");
                 String extn=fname.substring(dotLoc+1, fname.length()).trim();
                 System.out.println("extn: "+extn);
                 if(extn.equals("html")||extn.equals("htm"))
                 {
                     EditHtml editHtml=new EditHtml(fname);
                     while(keys.hasMoreElements())
                     {
                         String key=keys.nextElement().toString();
                         if(key.indexOf("attribIf")!=-1)
                         {
                            String[] srVal=prop.get(key).toString().split(",");
                            editHtml.setIfAttrib(srVal[0],srVal[1],srVal[2]);
                         }                      
                     }
                     editHtml.writeToFile();
                 }//if-html
                else if(extn.equals("xml")||extn.equals("jnlp"))
                 {
                     EditXml editXml=new EditXml(fname);
                     while(keys.hasMoreElements())
                     {
                         String key=keys.nextElement().toString();
                         if(key.indexOf("attribC")!=-1)
                         {
                            String[] srVal=prop.get(key).toString().split(",");
                            editXml.setAttrib(srVal[0],srVal[1],srVal[2]);
                         }
                         else if(key.indexOf("insert")!=-1)
                         {
                            String[] srVal=prop.get(key).toString().split(",");
                            editXml.addChild(srVal[0],srVal[1],srVal[2]);
                         }
                         else if(key.indexOf("childvalue")!=-1)
                         {
                            String[] srVal=prop.get(key).toString().split(",");
                            editXml.setChildValue(srVal[0],srVal[1],srVal[2]);
                         }
                         else if(key.indexOf("ifvalue")!=-1)
                         {
                            String[] srVal=prop.get(key).toString().split(",");
                            editXml.setIfChildValue(srVal[0],srVal[1],srVal[2],srVal[3],srVal[4]);
                         }
                       }
                     editXml.writeToFile();
                }//if-xml
             }
             catch (FileNotFoundException e)
             {
                 System.out.println(e.getMessage());
                 System.out.println("Usage: java EditConfig <confName>");
             }
        }//if
    }//main
}